package com.hzit.order.config;

import com.hzit.order.interceptor.LoginInterceptor;

import com.ruoyi.common.core.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 类名：
 * 作者：WF
 * 功能：配置登录拦截器
 */
@Configuration
public class HzitWebConfig implements WebMvcConfigurer {

	@Autowired
	private LoginInterceptor loginInterceptor;
	@Autowired
	private ThreadPoolProperties poolProperties;
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(loginInterceptor).addPathPatterns("/**");
	}

	//2. 构造线程池
	@Bean
	public ThreadPoolExecutor threadPoolExecutor(){
		ThreadPoolExecutor executor = new ThreadPoolExecutor(
				poolProperties.getCoreSize(),
				poolProperties.getMaxSize(),poolProperties.getKeepAliveTime(),
				TimeUnit.SECONDS,
				new LinkedBlockingDeque<>(),
				Executors.defaultThreadFactory(),
				new ThreadPoolExecutor.AbortPolicy()
		);
		return executor;
	}
	@Bean
	public IdWorker idWorker(){
		return new IdWorker();
	}


}
